package com.example.whatrubbish.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.whatrubbish.model.Rubbish;
import com.example.whatrubbish.service.RubbishService;
import com.example.whatrubbish.utils.Constants;
import com.example.whatrubbish.utils.JsonResult;
import com.example.whatrubbish.vo.Criteria;
import com.example.whatrubbish.vo.PageBean;
import com.example.whatrubbish.vo.RubbishWithSort;
import jdk.nashorn.internal.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/rubbish")
public class RubbishController {
    @Autowired
    RubbishService rubbishService;

    @ResponseBody
    @RequestMapping("/select")
    public JsonResult selectAllWithCount(HttpServletRequest request, HttpServletResponse response){
        JsonResult jsonResult;
        try{
            Integer pageIndex = request.getParameter("pageIndex")!=null?request.getParameter("pageIndex")!=""? Integer.parseInt(request.getParameter("pageIndex")) : null : null;
            Integer pageSize = request.getParameter("pageSize")!=null? request.getParameter("pageSize")!=""? Integer.parseInt(request.getParameter("pageSize")) : null : null;
            Criteria criteria = new Criteria();
            PageBean<RubbishWithSort> pageBean = new PageBean();
            if(pageIndex != null && pageSize != null){
                pageBean.setPageIndex(pageIndex);
                pageBean.setPageSize(pageSize);
                criteria.addExtField("pageIndex", (pageIndex-1) * pageSize);
                criteria.addExtField("pageSize", pageBean.getPageSize());
            }
            criteria.addExtField("sortId", request.getParameter("sortId")!=null? request.getParameter("sortId")!=""? request.getParameter("sortId"):null : null);
            criteria.addExtField("name", request.getParameter("name")!=null?request.getParameter("name")!=""? "%"+request.getParameter("name")+"%" :null : null);
            List<RubbishWithSort> list = rubbishService.selectBySortId(criteria);
            Long total = rubbishService.selectCountBySortId(criteria);
            pageBean.setTotalCount(total);
            pageBean.setResultList(list);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", pageBean);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("/recommend")
    public JsonResult selectHotWord(HttpSession session){
        JsonResult jsonResult;
        try{
            List list = rubbishService.selectRecommend();
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", list);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("/distribute")
    public JsonResult rubbishDistribute(HttpSession session){
        JsonResult jsonResult;
        try{
            List<Map<String, Object>> list = rubbishService.selectRubbishDistribute();
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", list);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(@RequestParam(value = "id") Long id){
        JsonResult jsonResult;
        try {
            int i = rubbishService.deleteByPrimaryKey(id);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "删除成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("/deleteByList")
    public JsonResult delete(@RequestBody Map map){
        JsonResult jsonResult;
        try {
            List<RubbishWithSort> delList = JSONArray.parseArray(map.get("delList").toString(), RubbishWithSort.class);
            int i = rubbishService.deleteByList(delList);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("/recover")
    public JsonResult recover(@RequestParam(value = "id") Long id){
        JsonResult jsonResult;
        try {
            int i = rubbishService.recoverByPrimaryKey(id);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "恢复成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("/update")
    public JsonResult recover(Rubbish record){
        JsonResult jsonResult;
        try {

            int i = rubbishService.updateByPrimaryKey(record);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("/insert")
    public JsonResult insert(Rubbish record){
        JsonResult jsonResult;
        try {

            int i = rubbishService.insertSelective(record);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }
}
